/*
 * Copyright (c) 2016 EEFUNG Software Co.Ltd. All rights reserved.
 * 版权所有(c)2016湖南蚁坊软件有限公司。保留所有权利。
 */

package com.zidian.leader.common.util;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.zidian.leader.MyApplication;

/**
 * 提供网络相关实用方法
 * Created by tanshengliang on 2015/11/20.
 */
public class NetworkUtils {

    private static final String NETWORK_STATE_MOBILE = "MOBILE"; // 手机流量数据网络

    private static final String NETWORK_STATE_WIFI = "WIFI"; // WIFI 网络

    private static final String NETWORK_STATE_NOT_CONNECTED = "NOT CONNECTED"; // WIFI 网络

    /**
     * 获取是否已经连接网络
     *
     * @return 是否已经连接网络
     */
    public static boolean isConnected() {
        ConnectivityManager connectiviyManager = (ConnectivityManager) MyApplication.getInstance().getSystemService(Context
                .CONNECTIVITY_SERVICE);
        return (connectiviyManager.getActiveNetworkInfo() != null && connectiviyManager.getActiveNetworkInfo().isAvailable());
    }

    /**
     * 获取是否已经使用 WIFI 连接网络
     *
     * @return 是否已经使用 WIFI 连接网络
     */
    public static boolean isConnectedOnWifi() {
        ConnectivityManager connectiviyManager = (ConnectivityManager) MyApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectiviyManager.getActiveNetworkInfo();
        return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI);
    }

    /**
     * 获取当前网络状态 区分：NOT CONNECTED / MOBILE / WIFI
     *
     * @return 当前网络状态
     */
    public static String getConnectState() {
        if (isConnected()) {
            if (isConnectedOnWifi()) {
                return NETWORK_STATE_WIFI;
            } else {
                return NETWORK_STATE_MOBILE;
            }
        }
        return NETWORK_STATE_NOT_CONNECTED;
    }
}